<template>
  <el-dialog
    v-model="modelValue"
    title="支付完成"
    width="450px"
    :show-close="false"
    destroy-on-close
  >
    <div class="payment-complete">
      <div class="success-icon">
        <i class="fa fa-check-circle"></i>
      </div>
      
      <div class="success-message">
        <div class="message-title">支付已成功</div>
        <div class="message-subtitle">您的订单已完成支付，感谢您的惠顾！</div>
      </div>
      
      <div class="order-info">
        <div class="info-row">
          <span class="info-label">订单号</span>
          <span class="info-value">{{ orderInfo?.orderNo }}</span>
        </div>
        
        <div class="info-row">
          <span class="info-label">交易时间</span>
          <span class="info-value">{{ orderInfo?.createTime }}</span>
        </div>
        
        <div class="info-row">
          <span class="info-label">支付方式</span>
          <span class="info-value">{{ getPaymentMethodName(orderInfo?.paymentMethod) }}</span>
        </div>
        
        <div class="info-row">
          <span class="info-label">订单金额</span>
          <span class="info-value">¥{{ formatPrice(orderInfo?.totalAmount) }}</span>
        </div>
        
        <div class="info-row" v-if="orderInfo && orderInfo.change > 0">
          <span class="info-label">实收金额</span>
          <span class="info-value">¥{{ formatPrice(orderInfo?.paidAmount) }}</span>
        </div>
        
        <div class="info-row" v-if="orderInfo && orderInfo.change > 0">
          <span class="info-label">找零</span>
          <span class="info-value change">¥{{ formatPrice(orderInfo?.change) }}</span>
        </div>
      </div>
      
      <div class="print-notice" v-if="isPrinting">
        <i class="fa fa-print"></i>
        <span>小票正在打印中，请稍候...</span>
      </div>
      
      <div class="completion-actions">
        <el-button 
          type="primary" 
          @click="handlePrintReceipt"
          :disabled="isPrinting"
        >
          <i class="fa fa-print"></i> 重新打印小票
        </el-button>
        
        <el-button 
          type="success" 
          @click="$emit('continue-shopping')"
        >
          <i class="fa fa-shopping-cart"></i> 继续购物
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, PropType, ref } from 'vue'

interface OrderInfo {
  orderNo: string;
  totalAmount: number;
  paidAmount: number;
  change: number;
  paymentMethod: string;
  createTime: string;
}

export default defineComponent({
  name: 'PaymentCompleteDialog',
  props: {
    modelValue: {
      type: Boolean,
      required: true
    },
    orderInfo: {
      type: Object as PropType<OrderInfo | null>,
      default: null
    }
  },
  emits: ['update:modelValue', 'print-receipt', 'continue-shopping'],
  setup(props, { emit }) {
    const isPrinting = ref(false)
    
    // 格式化价格
    const formatPrice = (price?: number) => {
      if (price === undefined) return '0.00'
      return price.toFixed(2)
    }
    
    // 获取支付方式显示名称
    const getPaymentMethodName = (method?: string) => {
      if (!method) return ''
      
      const methodMap: Record<string, string> = {
        cash: '现金',
        wechat: '微信支付',
        alipay: '支付宝',
        card: '银行卡'
      }
      
      return methodMap[method] || method
    }
    
    // 处理打印小票
    const handlePrintReceipt = () => {
      isPrinting.value = true
      
      emit('print-receipt')
      
      // 3秒后重置打印状态
      setTimeout(() => {
        isPrinting.value = false
      }, 3000)
    }
    
    return {
      isPrinting,
      formatPrice,
      getPaymentMethodName,
      handlePrintReceipt
    }
  }
})
</script>

<style scoped lang="scss">
.payment-complete {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  
  .success-icon {
    font-size: 60px;
    color: #67c23a;
    margin-bottom: 15px;
  }
  
  .success-message {
    text-align: center;
    margin-bottom: 20px;
    
    .message-title {
      font-size: 20px;
      font-weight: 500;
      color: #303133;
      margin-bottom: 5px;
    }
    
    .message-subtitle {
      font-size: 14px;
      color: #606266;
    }
  }
  
  .order-info {
    width: 100%;
    background-color: #f5f7fa;
    border-radius: 4px;
    padding: 15px;
    margin-bottom: 20px;
    
    .info-row {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .info-label {
        color: #606266;
        font-size: 14px;
      }
      
      .info-value {
        font-weight: 500;
        color: #303133;
        
        &.change {
          color: #f56c6c;
        }
      }
    }
  }
  
  .print-notice {
    display: flex;
    align-items: center;
    color: #e6a23c;
    background-color: #fdf6ec;
    padding: 8px 12px;
    border-radius: 4px;
    margin-bottom: 20px;
    font-size: 14px;
    
    i {
      margin-right: 5px;
    }
  }
  
  .completion-actions {
    display: flex;
    gap: 10px;
    
    .el-button {
      min-width: 130px;
    }
  }
}
</style> 